﻿using System;
using System.Linq;

using NUnit.Framework;
using Yala.UnitTests.IocSetup;
using Yala.UnitTests.UnitTestBase;
using Yala.Models.AuthProviders;

namespace Yala.UnitTest.Repositories {
    [TestFixture]
    public class AuthProvidersRepositoryTest : DbUnitTestBase {

        const string GUID_STRING = "ca660856-b4d5-429c-b161-594b6aad1aed";
        const string DELETE_GUID_STRING = "02ee4e2a-4777-46d6-bd46-8f8c1fc1bf84";
        IAuthProvidersRepository _repo;

        public AuthProvidersRepositoryTest() {
            Bootstrapper.ConfigureStructureMap();
        }

        [TestFixtureSetUp]
        public void Init() {
            DatabaseFixtureSetUp();
        }

        [SetUp]
        public void SetUp() {
            DatabaseSetUp();
            _repo = StructureMap.ObjectFactory.GetInstance<IAuthProvidersRepository>();
        }

        [TearDown]
        public void TearDown() {
            DatabaseTearDown();
            _repo.Dispose();
        }

        [TestFixtureTearDown]
        public void CleanUp() {
            DatabaseFixtureTearDown();
        }

        [Test]
        public void Can_Get_All_AuthProviders() {
            var providers = _repo.All();
            Assert.IsNotNull(providers);
            Assert.AreEqual(3, providers.Count());
        }

        [Test]
        public void Can_Get_AuthProvider_By_Id() {
            var providerId = Guid.Parse(GUID_STRING);
            var provider = _repo.Single(x => x.Id == providerId);
            Assert.IsNotNull(provider);
            Assert.AreEqual(GUID_STRING, provider.Id.ToString());
        }

        [Test]
        public void Can_Update_AuthProvider() {
            var providerId = Guid.Parse(GUID_STRING);
            var provider = _repo.Single(x => x.Id == providerId);

            // Update info
            provider.Name = "UpdatedProvider_1";
            _repo.Update(provider);

            // Get updated provider
            var updatedProvider = _repo.Single(x => x.Id == providerId);

            Assert.IsNotNull(updatedProvider);
            Assert.AreEqual(provider.Name, updatedProvider.Name);
        }

        [Test]
        public void Can_Get_AuthProvider_Users() {
            // Select provider where Id match the specify Id
            var providerId = Guid.Parse(GUID_STRING);
            var provider = _repo.Single(x => x.Id == providerId);

            Assert.IsNotNull(provider.Users);
            Assert.AreEqual(3, provider.Users.Count);
        }

        [Test]
        public void Can_Delete_AuthProvider_By_Id() {
            // Delete provider given it's Id
            var providerId = Guid.Parse(DELETE_GUID_STRING);
            _repo.Delete(x => x.Id == providerId);
            _repo.CommitChanges();

            // Now get all providers
            var providers = _repo.All();
            Assert.IsNotNull(providers);
            Assert.AreEqual(2, providers.Count());
        }

        [Test]
        public void Can_Delete_AuthProvider() {
            // Delete provider given it's Id
            var providerId = Guid.Parse(DELETE_GUID_STRING);
            var provider = _repo.Single(x => x.Id == providerId);
            _repo.Delete(provider);
            _repo.CommitChanges();

            // Now get all providers
            var providers = _repo.All();
            Assert.IsNotNull(providers);
            Assert.AreEqual(2, providers.Count());
        }

        private void GenerateXmlTestDataFile() {
            //We need to regenerate the xml test data
            //SaveTestDatabase();
        }
    }
}
